package com.wanxi.dangdang.controller.front.user;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dangdang.common.CommonResult;
import com.wanxi.dangdang.model.pojo.BUser;
import com.wanxi.dangdang.model.pojo.Role;
import com.wanxi.dangdang.model.vo.BUserAddVo;
import com.wanxi.dangdang.model.vo.BUserUpdateVo;
import com.wanxi.dangdang.model.vo.LoginVo;
import com.wanxi.dangdang.service.UserService;
import com.wanxi.dangdang.service.redis.RedisService;
import com.wanxi.dangdang.service.upload.UploadService;
import com.wanxi.dangdang.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/8/30 0030
 * @Version: 1.0
 */
@Api(tags = "B端用户前端管理 ")
@RestController("FrontUserController")
@RequestMapping("/front/b/user")
@Slf4j
public class UserController {

    @Value("${domainName}")
    private String domain;

    @Autowired
    private RedisService redisService;


    @Autowired
    private UserService userService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;


    @PostMapping("/loginByPass")
    @ApiOperation("B端用户账号密码登陆")
    private CommonResult loginByPass(@RequestBody LoginVo loginVo){
        log.info("B端用户账号密码登陆 数据"+ JSONObject.toJSONString(loginVo));
        if(StringUtils.isBlank(loginVo.getCodePicId())){
            return CommonResult.failed("验证码id错误");
        }
        String redisCode = redisService.get("codePicId-" + loginVo.getCodePicId());
        if (!StringUtils.equals(redisCode,loginVo.getPicCode()) || StringUtils.isBlank(loginVo.getPicCode())){
            return CommonResult.failed("验证码错误");
        }
        HashMap<String, String> map = userService.loginByPass(loginVo);

        if (ObjectUtils.isEmpty(map)){
            return CommonResult.failed("密码错误");
        }

        return CommonResult.successData(map);
    }

    @PostMapping("/loginByCode")
    @ApiOperation("B端用户手机验证码登陆")
    private CommonResult loginByCode(@RequestBody LoginVo loginVo){
        log.info("B端用户手机验证码登陆 数据"+ JSONObject.toJSONString(loginVo));
        String redisCode = redisService.get("codeTel-" + loginVo.getAccount());
        if (!StringUtils.equals(redisCode,loginVo.getTelCode())|| StringUtils.isBlank(loginVo.getTelCode())){
            return CommonResult.failed("验证码错误");
        }
        HashMap<String, String> map =  userService.loginByCode(loginVo);
        if (ObjectUtils.isEmpty(map)){
            return CommonResult.failed("该账号不存在/或已注销");
        }
        return CommonResult.successData(map);
    }

    @GetMapping("/findById")
    @ApiOperation("B端用户查询个人信息通过id（需要登录）")
    @ApiParam(value = "查询B端用户id", required = true)
    private CommonResult<BUser> findById(HttpSession httpSession,HttpServletRequest request){
        Long id = (Long)httpSession.getAttribute("id");
        if (id == null){
            CommonResult.failed("请登录");
        }
        BUser byId = userService.findById(id);
        return CommonResult.successData(byId);
    }

    @GetMapping("/findId")
    @ApiOperation("B端用户登录获取自己的id")
    @ApiParam(value = "查询B端用户id", required = true)
    private CommonResult<Long> findId(HttpSession httpSession){
        Long id = (Long)httpSession.getAttribute("id");
        if (id == null){
            CommonResult.failed("请登录");
        }
        return CommonResult.successData(id);
    }

    @PostMapping("/updateById")
    @ApiOperation("B端用户修改个人信息通过id（需要登录）")
    private CommonResult updateById(@RequestBody BUserUpdateVo bUserUpdateVo,HttpSession httpSession){
        log.info("B端用户手机验证码登陆 数据"+ JSONObject.toJSONString(bUserUpdateVo));
        Long id = (Long) httpSession.getAttribute("id");
        if (id == null){
            CommonResult.failed("请登录");
        }
        bUserUpdateVo.setId(id);
        userService.updateById(bUserUpdateVo);
        return CommonResult.success();
    }

    @PostMapping("/register")
    @ApiOperation("B端用户注册账号")
    private CommonResult register(@RequestBody BUserAddVo bUserAddVo){
        log.info("B端用户注册账号 数据"+ JSONObject.toJSONString(bUserAddVo));
        String redisCode = redisService.get("codeTel-" + bUserAddVo.getPhone());
        if (!StringUtils.equals(redisCode,bUserAddVo.getTelCode())|| StringUtils.isBlank(bUserAddVo.getTelCode())){
            return CommonResult.failed("验证码错误");
        }
        userService.add(bUserAddVo);
        return CommonResult.success();
    }

    @GetMapping("/phoneCode")
    @ApiOperation("B端用户手机号获取验证码")
    @ApiParam(value = "电话号码", required = true)
    private void phoneCode(@RequestParam("account") String phone) throws Exception {
        log.info("B端用户手机号获取验证码 数据"+ JSONObject.toJSONString(phone));
        userService.getPhoneCode(phone);
    }

    @GetMapping("/pictureCode")
    @ApiOperation("B端用户获取图片验证码")
    private CommonResult pictureCode() throws Exception {
        Map<String, String> pictureCode = userService.getPictureCode();
        return CommonResult.successData(pictureCode);
    }
}
